home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / InstallMaster 7.03 / Devfulldemo.exe / file0166.bin < prev    next >
Encoding:
Text File  |  1999-04-26  |  8.1 KB  |  263 lines

  1. //****************************************************************************
  2. // File: progress.c
  3. //
  4. // Purpose: example DLL file to display a custom progress bar
  5. //
  6. // Functions: LibMain, Update
  7. //
  8. //
  9. // Programmer:  John McMillan
  10. // Update to Win32:  Gary Chirhart
  11. //
  12. //****************************************************************************
  13.  
  14. #include <windows.h>
  15. #include "resource.h"
  16. #include "..\wisedll.h"
  17.  
  18. #define PROGRESS_BAR_TYPE_INIT 0
  19. #define PROGRESS_BAR_TYPE_SHOW 1
  20. #define PROGRESS_BAR_TYPE_HIDE 2
  21. #define PROGRESS_BAR_TYPE_DONE 3
  22.  
  23. #ifdef WIN32
  24. //#define huge
  25. #define __export __declspec( dllexport )
  26. #define __loadds
  27. #endif
  28.  
  29. HINSTANCE hDllInst;  // The instance of this DLL
  30. HWND hProgressDlg;   // The Progress dialog window handle
  31. HFONT hLightFont;    // A non-bold font for the dialog text
  32. int iWndPosition;    // A number from 0 to 8 indicating the postition of the progress dialog
  33. int iTotalPercent;   // The percentage (times 10) of the total installation
  34. int iFilePercent;    // The percentage (times 10) of the current file
  35. BOOL bCanceled;      // True if the user pressed the cancel button
  36. BOOL bHidden;        // The progress bar dialog is hidden
  37. BOOL bCentered;      // Indicates if the dialog has been centered
  38. POINT ptTotalStart,ptTotalEnd; // The upper left and lower right points of total progress bar
  39. POINT ptFileStart,ptFileEnd; // The upper left and lower right points of total progress bar
  40.  
  41. BOOL __export CALLBACK ProgressDlg(HWND, UINT, WPARAM, LPARAM);
  42. void CenterWindow(HWND hWnd);
  43. void GetEndPoints(HWND hDlg,UINT uID,POINT* pStart,POINT* pEnd);
  44. void DisplayBar(HDC hdc,POINT* ptStart,POINT* ptEnd,int iPercent);
  45. void SetLightFont(HWND hWnd);
  46. BOOL CALLBACK __loadds EnumChildCallback(HWND hWnd,LPARAM lParam);
  47.  
  48. //***********************************************************************
  49. // Function: LibMain
  50. //
  51. // Purpose: C function called from DLL entry point.
  52. //
  53. // Parameters: HINSTANCE hInst;   DLL instance handle
  54. //             WORD wSeg;         DLL data segment selector
  55. //             WORD cbHeapSize;   DLL initial heap size in bytes
  56. //             LPSTR lpszCmdLine; DLL command line
  57. //
  58. // Returns: 1 is success, 0 fail DLL load
  59. //
  60. // Comments:
  61. //
  62. // History:  Date       Author        Reason
  63. //
  64. //****************************************************************************
  65. #ifndef WIN32
  66. int CALLBACK LibMain(HINSTANCE hInst, WORD wDataSeg, WORD cbHeapSize, LPSTR lpszCmdLine)
  67. {
  68.    hDllInst = hInst;
  69.    return(1);
  70. }
  71. #endif
  72. #ifdef WIN32
  73. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,LPVOID lpvReserved)
  74. {
  75.    hDllInst = hinstDLL;
  76.    return TRUE;
  77. }
  78. #endif
  79. //***********************************************************************
  80. // Function: Update
  81. //
  82. // Purpose: Called to update the progress bar
  83. //
  84. // Parameters: 
  85. //
  86. //
  87. // Comments:
  88. //
  89. // History:  Date       Author        Reason
  90. //
  91. //****************************************************************************
  92.  
  93. HWND __export CALLBACK Update(int iType,int iPosition,LPDLLCALLPARAMS lpDllParams,char const* szDescr,int iPerInstall,int iPerFile)
  94. {
  95.    RECT rc;
  96.  
  97.    switch (iType) {
  98.     case PROGRESS_BAR_TYPE_INIT:
  99.       bHidden = TRUE;
  100.       return hProgressDlg = CreateDialog(hDllInst,MAKEINTRESOURCE(IDD_PROGRESS_DLG),(HWND)lpDllParams->hWnd,ProgressDlg);
  101.       break;
  102.     case PROGRESS_BAR_TYPE_SHOW:
  103.       if (bCanceled) {
  104.          bCanceled = FALSE; // Turn this off in case user chooses to continue
  105.          return hProgressDlg;
  106.       }
  107.       if ((szDescr == NULL) && bHidden) return NULL;
  108.       iWndPosition = iPosition;
  109.       if (!bCentered) {
  110.          CenterWindow(hProgressDlg);
  111.          bCentered = TRUE;
  112.       }
  113.       if (bHidden) {
  114.          EnableWindow(hProgressDlg,TRUE);
  115.          ShowWindow(hProgressDlg,SW_SHOW);
  116.          bHidden = FALSE;
  117.       }
  118.       if (szDescr != NULL) SetDlgItemText(hProgressDlg,IDC_STATIC_TEXT,szDescr);
  119.       iTotalPercent = iPerInstall;
  120.       iFilePercent = iPerFile;
  121.       rc.top = ptTotalStart.y - 1;
  122.       rc.bottom = ptTotalEnd.y + 1;
  123.       rc.left = ptTotalStart.x - 1;
  124.       rc.right = ptTotalEnd.x + 1;
  125.       InvalidateRect(hProgressDlg,&rc,FALSE);
  126.       rc.top = ptFileStart.y - 1;
  127.       rc.bottom = ptFileEnd.y + 1;
  128.       rc.left = ptFileStart.x - 1;
  129.       rc.right = ptFileEnd.x + 1;
  130.       InvalidateRect(hProgressDlg,&rc,FALSE);
  131.       break;
  132.     case PROGRESS_BAR_TYPE_HIDE:
  133.       EnableWindow(hProgressDlg,FALSE);
  134.       ShowWindow(hProgressDlg,SW_HIDE);
  135.       bHidden = TRUE;
  136.       break;
  137.     case PROGRESS_BAR_TYPE_DONE:
  138.       if (hLightFont != NULL) DeleteObject(hLightFont);
  139.       DestroyWindow(hProgressDlg);
  140.       break;
  141.    }
  142.    return NULL;
  143. }
  144.  
  145. BOOL __export CALLBACK ProgressDlg(HWND hDlg,UINT message,WPARAM wParam,LPARAM lParam)
  146. {
  147.    PAINTSTRUCT ps;
  148.    HDC hdc;
  149.  
  150.    switch (message) {
  151.     case WM_INITDIALOG:
  152.       SetLightFont(hDlg);
  153.       GetEndPoints(hDlg,IDC_STATIC_TOTAL,&ptTotalStart,&ptTotalEnd);
  154.       GetEndPoints(hDlg,IDC_STATIC_FILE,&ptFileStart,&ptFileEnd);
  155.       return TRUE;
  156.     case WM_PAINT:
  157.       hdc = BeginPaint(hDlg,&ps);
  158.       DisplayBar(hdc,&ptTotalStart,&ptTotalEnd,iTotalPercent);
  159.       DisplayBar(hdc,&ptFileStart,&ptFileEnd,iFilePercent);
  160.       EndPaint(hDlg,&ps);
  161.       break;
  162.     case WM_COMMAND:
  163.       bCanceled = TRUE;
  164.       break;
  165.    }
  166.    return FALSE;
  167. }
  168.  
  169. void CenterWindow(HWND hWnd)
  170. {
  171.    int i,j,x,y,iScreenX,iScreenY;
  172.    RECT rc;
  173.    HDC hDC;
  174.  
  175.    GetWindowRect(hWnd,&rc);
  176.    i = rc.right - rc.left;
  177.    j = rc.bottom - rc.top;
  178.    hDC = GetDC(NULL);
  179.    iScreenX = GetDeviceCaps(hDC,HORZRES);
  180.    iScreenY = GetDeviceCaps(hDC,VERTRES);
  181.    x = (((((iWndPosition & 0xf) % 3) + 1) * iScreenX) / 4) - (i / 2);
  182.    y = (((((iWndPosition & 0xf) / 3) + 1) * iScreenY) / 4) - (j / 2);
  183.    if ((x + i) > iScreenX) x = iScreenX - i;
  184.    if ((y + j) > iScreenY) y = iScreenY - j;
  185.    if (x < 0) x = 0;
  186.    if (y < 0) y = 0;
  187.    ReleaseDC(NULL,hDC);
  188.    MoveWindow(hWnd,x,y,i,j,FALSE);
  189. }
  190.  
  191.  
  192. void GetEndPoints(HWND hDlg,UINT uID,POINT* ptStart,POINT* ptEnd)
  193. {
  194.    HWND hBoxWnd;
  195.    RECT rc;
  196.  
  197.    hBoxWnd = GetDlgItem(hDlg,uID);
  198.    if (hBoxWnd != NULL) {
  199.       GetWindowRect(hBoxWnd,&rc);
  200.       ptStart->x = rc.left + 1;
  201.       ptStart->y = rc.top + 1;
  202.       ScreenToClient(hDlg,ptStart);
  203.       ptEnd->x = rc.right - 1;
  204.       ptEnd->y = rc.bottom - 1;
  205.       ScreenToClient(hDlg,ptEnd);
  206.    }
  207. }
  208.  
  209. void DisplayBar(HDC hdc,POINT* ptStart,POINT* ptEnd,int iPercent)
  210. {
  211.    SIZE sSize;
  212.    char ach[8];
  213.    RECT rc,rc1;
  214.    WORD wGomerX,wWidth,wHeight;
  215.    int iCurrPercent;
  216.  
  217.    wWidth = (WORD)(ptEnd->x - ptStart->x);
  218.    wHeight = (WORD)(ptEnd->y - ptStart->y);
  219.    rc.left = ptStart->x;
  220.    rc.right = ptStart->x + (WORD)(((DWORD)iPercent * (DWORD)wWidth) / (DWORD)1000);
  221.    rc.top = ptStart->y;
  222.    rc.bottom = ptEnd->y;
  223.    iCurrPercent = (iPercent + 5) / 10;
  224.    wsprintf(ach,"%3d%%",iCurrPercent);
  225.    GetTextExtentPoint(hdc,ach,wGomerX = lstrlen(ach),&sSize);
  226.    SetBkColor(hdc,RGB(0,0,127));
  227.    SetTextColor(hdc,RGB(192,192,192));
  228.    ExtTextOut(hdc, (wWidth - sSize.cx) / 2 + ptStart->x,
  229.               (wHeight - sSize.cy) / 2 + ptStart->y,
  230.               ETO_OPAQUE | ETO_CLIPPED, &rc, ach, wGomerX, NULL);
  231.    SetBkColor(hdc,RGB(192,192,192));
  232.    SetTextColor(hdc,RGB(0,0,127));
  233.    rc1 = rc;
  234.    rc1.left = rc.right + 1;
  235.    rc1.right = ptEnd->x;
  236.    ExtTextOut(hdc, (wWidth - sSize.cx) / 2 + ptStart->x,
  237.               (wHeight - sSize.cy) / 2 + ptStart->y,
  238.               ETO_OPAQUE | ETO_CLIPPED, &rc1, ach, wGomerX, NULL);
  239. }
  240.  
  241. void SetLightFont(HWND hWnd)
  242. {
  243.    HFONT hDialogFont;
  244.    LOGFONT dlgFont;
  245.  
  246.    if (hLightFont == NULL) {
  247.       hLightFont = NULL;
  248.       if ((hDialogFont = (HFONT) SendMessage(hWnd,WM_GETFONT,0,0)) != NULL) {
  249.          if (GetObject(hDialogFont,sizeof(dlgFont),&dlgFont)) {
  250.             dlgFont.lfWeight = FW_LIGHT;
  251.             hLightFont = CreateFontIndirect(&dlgFont);
  252.          }
  253.       }
  254.    }
  255.    if (hLightFont != NULL) EnumChildWindows(hWnd,EnumChildCallback,(LPARAM)hLightFont);
  256. }
  257.  
  258. BOOL CALLBACK __loadds EnumChildCallback(HWND hWnd,LPARAM lParam)
  259. {
  260.    SendMessage(hWnd,WM_SETFONT,(WPARAM)lParam,0L);
  261.    return TRUE;
  262. }
  263.